package com.study.happystudy.service.impl;

import com.study.happystudy.mapper.UserMapper;
import com.study.happystudy.po.Rank;
import com.study.happystudy.po.User;
import com.study.happystudy.service.UserService;
import com.study.happystudy.tool.MD5Tool;
import com.study.happystudy.tool.SaltGenerator;
import com.study.happystudy.tool.UniqueIdGenerator;
import io.swagger.models.auth.In;
import jakarta.annotation.Resource;
import lombok.Data;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务层
 *
 * @author wfs
 * @version 1.0
 * @date 2023/5/10 10:42
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;

    @Resource
    MD5Tool md5Tool;

    @Override
    public User Login(String phone, String userpassword) {
        try {
            String salt = userMapper.getUserSalt(phone);
            String pass = md5Tool.ToMD5(userpassword + salt);
            User user = userMapper.Login(phone, pass);
            if (user != null) {
                return user;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();

            return null;
        }
    }

    @Override
    public int upUserPwd(String phone, String userpassword) {
        try {
            String salt = userMapper.getUserSalt(phone);
            String pass = md5Tool.ToMD5(userpassword + salt);
            return userMapper.upUserPwd(phone, pass);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int register(String type, String name, String phone, String password, String institution, char gender) {

        try {
            String salt = SaltGenerator.generateSalt(24);
            String pass = md5Tool.ToMD5(password + salt);
            return userMapper.register(type, UniqueIdGenerator.generateUniqueId(), name, phone, pass, institution, salt, gender);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public boolean updateUser(String nickname, String headPortrait, String introduction, int age, String institution, char gender, int id) {
        User u = userMapper.findUserById(id);
        if (u == null) return false;// not found
        userMapper.updateUser(nickname, headPortrait, introduction, age, institution, gender, id);
        return true;
    }

    public User findUserById(Integer id) {
        return userMapper.findUserById(id);
    }

    @Override
    public void updatePoints(Integer id, Integer points) {
        userMapper.updatePoints(id,points);
    }

    @Override
    public boolean deleteUser(Integer id) {
        User user = userMapper.findUserById(id);
        if (user == null) return false;
        userMapper.deleteUser(id);
        return true;
    }

    @Override
    public List<User> findUserBySearchTerm(String searchTerm) {
        List<User> res = userMapper.findUserByEqual(searchTerm);
        if (res.isEmpty()) res = userMapper.findUserByContains(searchTerm);
        return res;
    }

    public List<Rank> getFriendRank(int uid){
        return userMapper.getFriendRank(uid);
    }
    public List<Rank> getAllRank(){
        List<Rank> allRank = userMapper.getAllRank();
        allRank.sort((o1, o2) -> o2.getStudentPoints() - o1.getStudentPoints());
        return allRank;
    }

    public void insertPooh(String question){
//        userMapper.insertPooh(question);
    }
}
